package cn.tedu.lucky.draw.server.job.config;

import cn.tedu.lucky.draw.server.job.listener.ActivityJobListener;
import cn.tedu.lucky.draw.server.job.processor.ActivityItemProcessor;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.repository.ActivityRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.PathResource;

import java.io.File;

/**
 * 作业配置：将csv文件读取到数据库
 */
@Configuration
public class Csv2DBJobConfig {
    /**
     * Job构建工厂
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private ActivityRepository activityRepository;

    //定义ItemReader
    @Bean
    public FlatFileItemReader<Activity> itemReader(){
        return new FlatFileItemReaderBuilder<Activity>()
                .name("activityItemReader")//配置实例名称
                .saveState(false)//禁用状态保存
                //配置资源,提供文件读取路径，此处使用绝对路径
                .resource(new PathResource(new File("doc/activity.csv").getAbsolutePath()))
                .delimited()//指定分隔符为逗号
                .names("name","description","img","status","sort")//指定字段名
                .targetType(Activity.class)//指定目标类型
                .linesToSkip(1)//读取跳过第一行
                .build();
    }

    //定义ItemWriter
    @Bean
    public ItemWriter<Activity> activityItemWriter(ActivityRepository repository){
        RepositoryItemWriter<Activity> itemWriter = new RepositoryItemWriter<>();
        itemWriter.setRepository(repository);
        itemWriter.setMethodName("save");
        return itemWriter;
    }

    //注入ItemProcessor
    @Bean
    public ActivityItemProcessor activityItemProcessor(){
        return new ActivityItemProcessor();
    }

    //注入监听器listener
    @Bean
    public ActivityJobListener activityJobListener(){
        return new ActivityJobListener();
    }

    @Bean
    public Job csvToDBJob() {
        return jobBuilderFactory.get("csv2DB-step-job")
                //调用start()配置作业步骤
                .start(csvToDBStep())
                .incrementer(new RunIdIncrementer())
                .listener(activityJobListener())
                .build();
    }

    @Bean
    public Step csvToDBStep() {
        return stepBuilderFactory.get("csv2DB-step")
                .<Activity,Activity>chunk(10000)
                .reader(itemReader())
                .processor(activityItemProcessor())
                .writer(activityItemWriter(activityRepository))
                .build();
    }
}
